package org.zhao.nettydemos.jedis;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import redis.clients.jedis.Jedis;

/**
 * @Auther: xiaohui.zhao
 * @Date: 2021/9/18 08:51
 * @Description:  使用jedis 操作redis中的String
 */
@SpringBootTest
@RunWith(SpringRunner.class)
@Slf4j
public class JedisStringDemo {

    @Test
    public void operateString(){
        // 连接本地的6379端口的redis ， 密码是jx123 , 数据库选择第一个
        Jedis jedis = new Jedis("localhost",6379);
        jedis.auth("jx123");
        jedis.select(0);
        // 测试连接
        log.info("jedis.ping():"+jedis.ping());

        // 开始操作
        jedis.set("key0","123456");
        log.info(jedis.get("key0"));

        // 检查key0是否存在
        log.info("key0 exists = :"+jedis.exists("key0"));

        // 返回key0长度
        log.info("key0 strlen = "+jedis.strlen("key0"));

        // 返回截取字符串 0,-1表示截取全部
        log.info("jedis.getrange(key0) = "+jedis.getrange("key0",0,-1));

        log.info("jedis.getrange(key0,1,4) = "+jedis.getrange("key0",1,4));

        // 追加字符串
        log.info("jedis.append(key0) = "+jedis.append("key0","张三李四王麻子"));

        log.info("after append = "+jedis.get("key0"));

        // 重命名
        jedis.rename("key0","key0_new");

        // 判断oldkey是否存在
        log.info("jedis.exists(key0) = "+jedis.exists("key0"));

        // 批量插入
        jedis.mset("key1","张三","key2","李四","key3","王麻子");
        log.info("批量取出  : "+jedis.mget("key1","key2","key3","key0"));
    }

}